﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Configuration;
using System.Data.SqlClient;
using System.Data.Sql;
using System.Windows.Forms;

namespace StudentApartMentManage
{
    public partial class ManualAssignment : Form
    {
        public ManualAssignment()
        {
            InitializeComponent();
        }
        void show()
        {
            comboBox1.ValueMember = "学号";
            comboBox1.DisplayMember = "学院";
            comboBox1.DataSource = sqlHelper.GetTable("select distinct 学院 from 学生信息表");
            comboBox2.ValueMember = "学号";
            comboBox2.DisplayMember = "专业";
            comboBox2.DataSource = sqlHelper.GetTable("select distinct 专业 from 学生信息表");
            comboBox3.ValueMember = "学号";
            comboBox3.DisplayMember = "班级";
            comboBox3.DataSource = sqlHelper.GetTable("select distinct 班级 from 学生信息表");
            comboBox4.ValueMember = "学号";
            comboBox4.DisplayMember = "民族";
            comboBox4.DataSource = sqlHelper.GetTable("select distinct 民族 from 学生信息表");

        }
        private void ManualAssignment_Load(object sender, EventArgs e)
        {
            show();
            dataGridView3.DataSource = DataShow.show("select * from 公寓分配表");
        }

        private void button1_Click(object sender, EventArgs e)
        {
            //查询符合条件的宿舍
            string sql = string.Format("exec  getDorm '{0}%','{1}%','{2}%','{3}%','{4}'", comboBox1.Text, comboBox2.Text, comboBox3.Text, comboBox4.Text, comboBox5.Text);
            string connectionString = ConfigurationManager.ConnectionStrings["con"].ConnectionString;
            SqlConnection con = new SqlConnection(connectionString);
            SqlCommand cmd = new SqlCommand(sql, con);
            con.Open();
            SqlDataReader r = cmd.ExecuteReader();
            if (r.Read())
            {
                MessageBox.Show("查询成功!");
                dataGridView1.DataSource = sqlHelper.GetTable(sql);
                
            }
            else
            {

                MessageBox.Show("未能匹配到宿舍,请选择自动分配!");
                r.Close();
                con.Close();
                return;
            }
            con.Close();
            dataGridView1.DataSource = sqlHelper.GetTable(sql);
        }

        private void bt3_Click(object sender, EventArgs e)
        {
            if (MessageBox.Show("你真的要重置查找信息吗?", "警告", MessageBoxButtons.YesNoCancel) == DialogResult.Yes)
            {
                comboBox1.Text = "";
                comboBox2.Text = "";
                comboBox3.Text = "";
                comboBox4.Text = "";
                comboBox5.Text = "";

            }
        }

        private void comboBox5_SelectedIndexChanged(object sender, EventArgs e)
        {

        }

        private void button2_Click(object sender, EventArgs e)
        {
            string sql = string.Format("select * from 学生信息表 where 学号='{0}'", textBox1.Text.Trim());
            dataGridView2.DataSource = sqlHelper.GetTable(sql);
        }

        private void bt1_Click(object sender, EventArgs e)
        {
            //手工分配
            if (textBox1.Text.Length != 0)
            {
                if (MessageBox.Show("你真的要手工分配宿舍吗?", "警告", MessageBoxButtons.YesNoCancel) == DialogResult.Yes)
                {
                    string sql = string.Format("exec zdinsert '{0}','{1}', '{2}'", dataGridView1.SelectedCells[0].Value.ToString(), dataGridView1.SelectedCells[1].Value.ToString(), dataGridView2.SelectedCells[0].Value.ToString());
                    int i = sqlHelper.Execute(sql);
                    if (i != -1)
                        MessageBox.Show("分配成功!");
                    else
                        MessageBox.Show("分配失败!");
                    show();
                    dataGridView3.DataSource = DataShow.show("select * from 公寓分配表");
                }
            }
        }
    }
}
